//
//  HomepageViewController.swift
//  zhihuifan
//
//  Created by apple on 2017/8/10.
//  Copyright © 2017年 youyou. All rights reserved.
//

import UIKit
private let HmpgHeadSectionReuseId = "HomepageHeaderSectionViewReuseId"
private let HpTuijianTableViewCellId = "HpTuijianTableViewCellId"
private let HotshopReuseId = "hotshopReuseId"
class HomepageViewController: ZHFBaseViewController {
    //首页页面
    lazy var tableview:UITableView = UITableView(frame: CGRect(x: 0, y: 93, width: ScreenWith, height: ScreenHeight - 113), style: .grouped)
    lazy var reccommendViewModel:RecommendShopViewModel = RecommendShopViewModel()
    lazy var hotShopViewModel:HotShopViewModel = HotShopViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
extension HomepageViewController {
    fileprivate func setupUI(){
        tableview.delegate = self
        tableview.dataSource = self
        tableview.register(UINib(nibName: "HomepageHeaderSectionView", bundle: nil)
, forHeaderFooterViewReuseIdentifier:HmpgHeadSectionReuseId)
       // tableview.separatorStyle = .none
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "resuId")
        tableview.register(UINib(nibName: "HpTuijianTableViewCellId", bundle: nil), forCellReuseIdentifier: HpTuijianTableViewCellId)
        tableview.register(UINib(nibName: "HotShopTableViewCell", bundle: nil), forCellReuseIdentifier: HotshopReuseId)
        //navbar
        let navbar = HomepageNavBar.loadNib()
        navbar.qrcodeView.addTarget(self, action: #selector(qrcodeAct), for: .touchUpInside)
        navbar.searchView.addTarget(self, action: #selector(searchAct), for: .touchUpInside)
        navbar.clickLocationCallBack = {
            print("点击位置选择")
        }
        navbar.frame = CGRect(x: 0, y: 0, width: ScreenWith, height: 93)
        view.addSubview(navbar)
        view.addSubview(tableview)
    }
  fileprivate func loadData(){
    //推荐商家
        reccommendViewModel.loadRecommendData { (isSuccess) in
            self.tableview.reloadData()
    }
    //热门商家
    hotShopViewModel.loadHotShopData{ (isSucess) in
        self.tableview.reloadData()
    }
    }
}
//MARK:-表格数据源方法
extension HomepageViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return reccommendViewModel.recommendModelList.count
        }else if section == 1{
            return 5
        }else if section == 2{
            return 5
        }
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            var cell:TuiJianTableViewCell?

            if cell == nil {
                cell = TuiJianTableViewCell.loadNib()
                
            }else{
                cell = tableview.dequeueReusableCell(withIdentifier: HpTuijianTableViewCellId, for: indexPath) as? TuiJianTableViewCell
            }
            let model = reccommendViewModel.recommendModelList[indexPath.row]
            cell?.recommendModel = model
            return cell!
        }else if indexPath.section == 1{
            var hotCell:HotShopTableViewCell?
            if hotCell == nil {
                hotCell = HotShopTableViewCell.loadNib()
            }else{
             hotCell = tableView.dequeueReusableCell(withIdentifier: HotshopReuseId, for: indexPath) as? HotShopTableViewCell
            }
            hotCell?.hotshopModel = hotShopViewModel.hotShopModelList
            return hotCell!
        }
        else{
            var cell:UITableViewCell?

            cell = tableview.dequeueReusableCell(withIdentifier: "resuId", for: indexPath)
            cell?.textLabel?.text = "第\(indexPath.row)行"
            return cell!
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 125
        }else if indexPath.section == 1{
            return 185
        }
    
        return 50
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
      
        return 10
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 206
        }
        return 41
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            let sectionView = HomepageHeaderSectionView.loadNib()
            sectionView.scrollView.contentSize = CGSize(width: ScreenWith * 2, height: 206)
            return sectionView
        }
        return nil
    }
}
//MARK:-navbar方法
extension HomepageViewController{
    @objc fileprivate func qrcodeAct(){
       print("点击进入扫描界面")
    }
    @objc fileprivate func searchAct(){
        print("点击进入搜索界面")
    }
}
